package com.newworld.basketballfire.service.controller;

import com.newworld.basketballfire.service.delegate.UserServiceDelegate;
import com.newworld.basketballfire.service.exception.ServiceException;
import com.newworld.basketballfire.service.model.ListResult;
import com.newworld.basketballfire.service.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Created by George on 2019/3/3.
 * UserService
 */
@RequestMapping(value = "/rest/user")
@Controller
public class UserService {
    @Autowired
    private UserServiceDelegate delegate;

    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public User createUser(@RequestBody User user) throws ServiceException {
        return delegate.createUser(user);
    }

    @RequestMapping(method = RequestMethod.PUT)
    @ResponseBody
    public User updateUser(@RequestBody User user) throws ServiceException {
        return delegate.updateUser(user.getId(), user);
    }

    @RequestMapping(method = RequestMethod.PUT, value = "/{id}")
    @ResponseBody
    public User updateUserById(@PathVariable String id, @RequestBody User user) throws ServiceException {
        return delegate.updateUser(id, user);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/{id}")
    @ResponseBody
    public User getUserById(@PathVariable String id) throws ServiceException {
        return delegate.getUser(id);
    }

    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public User getUser(@RequestParam String id) throws ServiceException {
        return delegate.getUser(id);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/list")
    @ResponseBody
    public ListResult<User> getUserList(@RequestParam(required = false, defaultValue = "") String name,
                                        @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                        @RequestParam(required = false, defaultValue = "10") Integer pageSize) throws ServiceException {
        return delegate.getUserList(name, pageNo, pageSize);
    }
}
